var AdsAdd = function() {

	var handleValidation = function() {
		var formAdd = $('#addForm');
		var validator = formAdd.validate({
			rules : {
				adsName : {
					required : true,
					rangelength : [ 1, 50 ]
				},
				orders : {
					required : true,
					number : true
				}
			},
			messages : {
				adsName : {
					required : "请输入广告名称",
					rangelength : "长度范围为1-50"
				},
				orders : {
					required : "排序值不能为空",
					number : "请输入一个数字"
				}
			},
			success : function(label, element) {
				var icon = $(element).parent('.input-icon').children('i');
				// set success class to the control group
				$(element).closest('.form-group').removeClass('has-error')
						.addClass('has-success');
				icon.removeClass("fa-warning").addClass("fa-check");
			},

			submitHandler : function(form) {
				var fileUrl = $("#thumbnail").val();
				if (fileUrl == null || fileUrl == "") {
					bootbox.alert("请添加一张广告图片");
					return false;
				}
				var url = $("#url").val();
				if ($("#link").val() == "1") {
					if (url == "") {
						bootbox.alert("请添加跳转链接");
						return false;
					}else{
						var start = url.indexOf("http://");
						var starts = url.indexOf("https://");
						if(!(start == 0 || starts == 0)){
							bootbox.alert("跳转链接不符合规则");
							return false;
						}
						$("#productId").val("");
					}
				}
				var json = $('form#addForm').serializeJSON();
				$('#addModel').attr('disabled', true);
				$.ajax({
					type : "post",
					contentType : "application/json; charset=UTF-8",
					url : ctx + "/noads/doAdd",
					data : json,
					success : function(msg) {
						if (msg.code == 1) {
							bootbox.dialog({
								message : "添加广告成功！",
								title : "温馨提示：",
								buttons : {
									main : {
										label : "确定",
										className : "btn btn-primary",
										callback : function() {
											window.location.href = 'noads/add';
										}
									}
								}
							});
							$('#addModel').attr('disabled', false);
						} else {
							bootbox.dialog({
								message : msg.message,
								title : "温馨提示：",
								buttons : {
									main : {
										label : "确定",
										className : "btn btn-primary",
										callback : function() {
											// window.location.reload();
										}
									}
								}
							});
							$('#addModel').attr('disabled', false);
						}
					},
					error : function(msg) {
						bootbox.alert(msg.message);
						$('#addModel').attr('disabled', false);
					}
				});
			}
		});
	}

	return {
		init : function() {
			handleValidation();
		}
	};
}();

$("#file").change(
		function() {
			data = new FormData();
			data.append("file", this.files[0]);
			$.ajax({
				data : data,
				type : "POST",
				url : ctx + "/noads/uploadFile",
				cache : false,
				contentType : false,
				processData : false,
				success : function(msg) {
					if (msg.code == 1) {
						$("#thumbnail").val(msg.data);
						$("#adsImg").html(
								'<img src="' + imagePrefix + msg.data
										+ '" width="256">');
					} else {
						bootbox.alert("添加图片失败！");
					}
				},
				error : function(msg) {
					bootbox.alert(msg.message);
				}
			});
		});